<?php
namespace app\index\controller;

use think\Controller;
use think\Db;

/**
 * 系统用户类
 */
class Channel extends Controller
{

    /**
     * 表字段初始化
     */
    public function _initialize()
    {

        //获取数据表所有信息
        $this->table = 'acc_dragon_channel';

        $tableInfo = Db::getTableInfo($this->table);

        $this->pk = $tableInfo["pk"];
        $this->order();

        parent::_initialize();
    }
    /**
     * 默认排序
     */
    public function order()
    {

        $this->order = $this->pk . ' asc';
    }
    /**
     * 分页查询
     */
    public function doJsonPList()
    {

        //默认分页查询数据
        $total = Db::table($this->table)
            ->field($this->pk)
            ->count();

        //排序
        if (!empty($this->order)) {
            $list = Db::table($this->table)
                ->order($this->order)
                ->select();
        }

        $res = array();

        if ($total > 0) {
            $res['rows']  = $list;
            $res['total'] = $total;
            _callback($res);

        } else {
            $res['total'] = 0;
            $res['rows']  = array();
            _callback($res);

        }
    }
    /**
     * 新建
     */
    public function doJsonPAdd()
    {

        //销毁元素
        unset($_REQUEST['_dc']);
        $temp = $_REQUEST['callback'];
        unset($_REQUEST['callback']);
        unset($_REQUEST['id']);

        $res = Db::table($this->table)->insert($_REQUEST);

        if ($res) {
            $output = array('success' => true, 'msg' => "新增成功！", 'data' => $res);
        } else {
            $output = array('success' => false, 'msg' => "新增失败！", 'data' => $res);
        }

        //恢复callback
        $_REQUEST['callback'] = $temp;
        _callback($output);
    }
    /**
     * 删除
     */
    public function doJsonPDelete()
    {

        if (array_key_exists('id', $_REQUEST) && !empty($_REQUEST['id'])) {

            $res = Db::table($this->table)->delete($_REQUEST['id']);

            if ($res) {
                $output = array('success' => true, 'msg' => "删除成功！", 'data' => $res);
            } else {
                $output = array('success' => false, 'msg' => "删除失败！", 'data' => $res);
            }
            _callback($output);
        }

    }
    /**
     * 更新
     */
    public function doJsonPUpdate()
    {

        //销毁元素
        unset($_REQUEST['_dc']);
        $temp = $_REQUEST;
        unset($_REQUEST['callback']);

        //更新记录
        $res = Db::table($this->table)->update($_REQUEST);

        if ($res) {
            $output = array('success' => true, 'msg' => "更新成功！", 'data' => $res);
        } else {
            $output = array('success' => false, 'msg' => "更新失败！", 'data' => $res);
        }

        //恢复$_REQUEST['callback']
        $_REQUEST = $temp;
        _callback($output);
    }
}
